package com.shycloud.mido.opter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.opter.api.entity.StockDeviceInfo;
import com.shycloud.mido.opter.api.entity.StockSaleOrder;
import com.shycloud.mido.opter.api.entity.StockSupplier;
import com.shycloud.mido.opter.api.vo.*;
import com.shycloud.mido.opter.service.StockOrderService;
import com.shycloud.mido.opter.api.entity.StockWarehouse;
import com.shycloud.mido.opter.service.StockDeviceInfoService;
import com.shycloud.mido.opter.service.StockExchangeGoodsService;
import com.shycloud.mido.opter.service.StockSupplierService;
import com.shycloud.mido.opter.service.StockWarehouseService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商模块 controller
 *
 * @author nianhua.jiang
 * @date 2020/9/22 14:26
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/stock")
public class StockController {

	/** 供应商模块 service */
	private StockSupplierService stockSupplierService;
	/** 仓库模块 service */
	private StockWarehouseService warehouseService;
	/** 智能设备模块 service */
	private StockDeviceInfoService deviceInfoService;
	/** 换货模块 service */
	private StockExchangeGoodsService exchangeGoodsService;
	/** 销售单和出库单 service */
	private StockOrderService stockOrderService;
	/** 七牛云上传 */
	private MinioTemplate minioTemplate;

	/**
	 * 新增/编辑供应商信息
	 *
	 * @param supplier
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/22 14:32
	 **/
	@PostMapping("saveSupplier")
	public R saveSupplier(@RequestBody StockSupplier supplier) {
		supplier.setCreateTime(new Date());
		return new R(stockSupplierService.saveOrUpdate(supplier));
	}

	/**
	 * 获取供应商列表
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/22 14:37
	 **/
	@GetMapping("getSupplier")
	public R getSupplier() {
		return new R(stockSupplierService.list());
	}

	/**
	 * 获取仓库名称列表
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/24 10:00
	 **/
	@GetMapping("getWareHouseList")
	public R getWareHouseList() {
		return new R(warehouseService.list());
	}

	/**
	 * 新增仓库
	 *
	 * @param stockWarehouse
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/24 10:01
	 **/
	@PostMapping("addWareHouse")
	public R addWareHouse(@RequestBody StockWarehouse stockWarehouse) {

		//判断仓库名称是否重复
		List<StockWarehouse> stockWarehouseList = warehouseService
				.list(new QueryWrapper<StockWarehouse>().eq("name", stockWarehouse.getName()));

		if (stockWarehouseList.size() > 0) {
			return new R(CommonConstants.FAIL, "仓库名称重复，请重新输入", false);
		}

		return new R(warehouseService.saveOrUpdate(stockWarehouse));
	}

	/**
	 * 获取硬件追溯列表
	 *
	 * @param udid        UDID
	 * @param nameOrPhone 客户名称或手机号
	 * @param status      状态
	 * @param type        设备类型
	 * @param user        激活用户 - 手机号/姓名
	 * @param current     当前页
	 * @param size        每页显示条数
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/24 15:44
	 **/
	@GetMapping("getDeviceInfoList")
	public R getDeviceInfoList(String udid, String nameOrPhone, String status, String type, String user, Integer current,
			Integer size) {
		return new R(deviceInfoService.getDeviceInfoList(udid, nameOrPhone, status, type, user, new Page(current, size)));
	}

	/**
	 * 新增换货信息
	 *
	 * @param exchangeGoodsVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/25 15:25
	 **/
	@PostMapping("addExchangeGoods")
	public R addExchangeGoods(@RequestBody StockExchangeGoodsVO exchangeGoodsVO) {
		return new R(exchangeGoodsService.addExchangeGoods(exchangeGoodsVO));
	}

	/**
	 * 编辑换货信息
	 *
	 * @param exchangeGoodsVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/25 15:25
	 **/
	@PostMapping("saveExchangeGoods")
	public R saveExchangeGoods(@RequestBody StockExchangeGoodsVO exchangeGoodsVO) {
		return new R(exchangeGoodsService.saveExchangeGoods(exchangeGoodsVO));
	}

	/**
	 * 获取换货列表
	 *
	 * @param name      客户或联系人名称
	 * @param startDate 开始日期
	 * @param endDate   结束日期
	 * @param isCommit  状态 0-未提交 1-已提交
	 * @param current   当前页
	 * @param size      每页显示条数
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/27 14:27
	 **/
	@GetMapping("getExchangeGoodsList")
	public R getExchangeGoodsList(String name, String startDate, String endDate, String isCommit, Integer current, Integer size) {
		return new R(exchangeGoodsService.getExchangeGoodsList(new Page(current, size), name, startDate, endDate, isCommit));
	}

	/**
	 * 删除换货
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/27 18:17
	 **/
	@GetMapping("delExchangeGoods")
	public R delExchangeGoods(Integer id) {
		return new R(exchangeGoodsService.removeById(id));
	}

	/**
	 * 提交换货单
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/29 14:52
	 **/
	@GetMapping("commitExchangeGoods")
	public R commitExchangeGoods(Integer id) {
		return new R(exchangeGoodsService.commitExchangeGoods(id));
	}

	/**
	 * 增加新销售单
	 *
	 * @param orderVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 12:23
	 */
	@PostMapping("addSaleOrder")
	public R addSaleOrder(@RequestBody StockSaleOrderVO orderVO) {
		return new R(stockOrderService.addSaleOrder(orderVO));
	}

	/**
	 * 增加新销售单
	 *
	 * @param detailVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 12:23
	 */
	@PostMapping("addSaleOrderDetail")
	public R addSaleOrderDetail(@RequestBody StockSaleOrderDetailVO detailVO) {
		return new R(stockOrderService.addSaleOrderDetail(detailVO));
	}

	/**
	 * 获取新销售单(未提交/已提交，未出库/已出库)
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/25 11:31
	 */
	@GetMapping("getSaleOrders")
	public R getSaleOrders(String commitStatus, String outStatus) {
		return new R(stockOrderService.getSaleOrders(commitStatus, outStatus));
	}

	/**
	 * 分页获取新销售单(未提交/已提交，未出库/已出库)
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/25 11:31
	 */
	@GetMapping("pageSaleOrders")
	public R pageSaleOrders(Integer current, Integer size,
			String commitStatus, String outStatus,
			String createdBy, String outType,
			String startDate, String endDate,
			String searchName, String comments, String processStatus, String orderNo) {
		return new R(stockOrderService.pageSaleOrders(
				new Page(current, size), commitStatus,
				outStatus, createdBy, outType,
				startDate, endDate, searchName, comments, processStatus, orderNo));
	}

	/**
	 * 删除新销售单
	 *
	 * @param saleOrderVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 12:23
	 */
	@PostMapping("deleteSaleOrder")
	public R deleteSaleOrder(@RequestBody StockSaleOrderVO saleOrderVO) {
		return new R(stockOrderService.deleteSaleOrder(saleOrderVO));
	}

	/**
	 * 提交销售单
	 *
	 * @param saleOrderVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 22:49
	 */
	@PostMapping("commitSaleOrder")
	@ApiOperation("提交销售单")
	public R commitSaleOrder(@RequestBody StockSaleOrderVO saleOrderVO) {
		return new R(stockOrderService.commitSaleOrder(saleOrderVO));
	}

	/**
	 * 编辑销售单
	 *
	 * @param saleOrderVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 22:49
	 */
	@PostMapping("editSaleOrder")
	@ApiOperation("提交销售单")
	public R editSaleOrder(@RequestBody StockSaleOrderVO saleOrderVO) {
		return new R(stockOrderService.editSaleOrder(saleOrderVO));
	}

	/**
	 * 出库发货
	 *
	 * @param saleOrderVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 22:49
	 */
	@PostMapping("sendOutOrder")
	@ApiOperation("出库发货")
	public R sendOutOrder(@RequestBody StockSaleOrderVO saleOrderVO) {
		return new R(stockOrderService.sendOutOrder(saleOrderVO));
	}

	/**
	 * 生成并获取设备码
	 *
	 * @param batchUdidVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 22:49
	 */
	@PostMapping("genBatchUdid")
	@ApiOperation("生成并获取设备码")
	public R generateBatchUdid(@RequestBody GenBatchUdidVO batchUdidVO) {
		return new R(stockOrderService.generateBatchUdid(batchUdidVO));
	}

	/**
	 * 获取udid信息
	 *
	 * @param udid
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 22:49
	 */
	@GetMapping("getUdidInfo")
	@ApiOperation("获取udid信息")
	public R generateBatchUdid(String udid) {
		return new R(stockOrderService.getUdidInfo(udid));
	}

	/**
	 * 删除销售单生成的UDID
	 *
	 * @param batchUdidVO
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/24 12:23
	 */
	@PostMapping("deleteUdidByOrderNo")
	public R deleteUdidByOrderNo(@RequestBody GenBatchUdidVO batchUdidVO) {
		return new R(stockOrderService.deleteUdidByOrderNo(batchUdidVO));
	}

	/**
	 * 维修处理(维修调拨，报废处理)
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/27 19:37
	 */
	@PostMapping("processService")
	public R processService(@RequestBody StockProcessDetailVO processVO) {
		return new R(stockOrderService.processService(processVO));
	}

	/**
	 * 分页获取待处理信息
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/10/16 14:07
	 */
	@GetMapping("pageProcessList")
	public R pageProcessList(Integer current, Integer size,
			String commitStatus, String outStatus,
			String createdBy, String outType,
			String startDate, String endDate,
			String searchName, String comments) {
		return new R(stockOrderService.pageProcessList(
				new Page(current, size), commitStatus,
				outStatus, createdBy, outType,
				startDate, endDate, searchName, comments));
	}

	/**
	 * 分页获取处理明细(维修调拨，报废处理)
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/27 19:37
	 */
	@GetMapping("pageProcessDetail")
	public R pageProcessDetail(Integer current, Integer size,
			String startDate, String endDate,
			String createdBy, String processType,
			Integer warehouseId, String goodsName,
			String comments) {
		return new R(stockOrderService.pageProcessDetail(
				new Page(current, size), startDate, endDate, createdBy,
				processType, warehouseId, goodsName, comments));
	}

	/**
	 * 获取硬件商品
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/28 16:07
	 */
	@GetMapping("getHardwareGoods")
	public R getHardwareGoods() {
		return new R(stockOrderService.getHardwareGoods());
	}

	/**
	 * 分页获取商品库存统计
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/27 19:37
	 */
	@GetMapping("pageStockAmount")
	public R pageGoodsStockAmount(Integer current, Integer size,
			Integer warehouseId, Integer goodsId,
			Integer categoryId) {
		return new R(stockOrderService.pageGoodsStockAmount(
				new Page(current, size), warehouseId, goodsId, categoryId));
	}

	/**
	 * 统计仓库总数据量
	 *
	 * @return
	 * @author xiaoxing.yu
	 * @date 2020/9/27 19:37
	 */
	@GetMapping("getAllStockInfo")
	public R getAllStockInfo() {
		return new R(stockOrderService.getAllStockInfo());
	}

	/**
	 * 获取运营商附件空间的 token
	 *
	 * @param bucketName
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2021/1/29 15:08
	 **/
	@GetMapping("getUploadToken")
	public R getUploadToken(String bucketName) {
		Map<String, Object> resultMap = new HashMap<>();
		//long overTime = 3600;
		if (StringUtils.isEmpty(bucketName)) {
			resultMap.put("token", minioTemplate.getPrivateUploadToken("mido-video-recording", null));
		} else {
			resultMap.put("token", minioTemplate.getPrivateUploadToken(bucketName, null));
		}

		resultMap.put("overTime", 3600);
		return new R(resultMap);
	}

	@PostMapping("addSaleOrderFeign")
	public R<Boolean> addSaleOrderFeign(@RequestBody StockSaleOrder saleOrder) {
		return new R(stockOrderService.save(saleOrder));
	}

	@PostMapping("saveSaleOrderFeign")
	public R<Boolean> saveSaleOrderFeign(@RequestBody StockSaleOrder saleOrder) {
		return new R(stockOrderService.updateById(saleOrder));
	}

	/**
	 * 通过udid停用设备
	 *
	 * @param udid    设备编号
	 * @param orderNo 订单编号
	 * @return {@link R}
	 * @author nianhua.jiang
	 * @date 2021/11/5 9:50
	 */
	@GetMapping("stopUdid")
	public R<Boolean> stopUdid(String udid, String orderNo) {
		return new R(stockOrderService.stopUdid(udid, orderNo));
	}

}
